﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using FinPlusCommon;
using u = FinPlusUtility.Utility;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusClienView : FinPlusControl
    {
        public override string Id { get; set; }
        public override string[] States { get { return new string[] { "Height", "Width", "Left", "Top" }; } }
        public override string[] TestStates { get { return new string[] { "ModelTest", "IsValid" }; } }
        public override IFunctions Model { get { return base.Children(grid.Children); } }
        public override string ModelTest { get { return Model.ToString(); } }
        public override bool IsValid { get; set; }

        private enum DockControls { Exception, Default }
        private enum Params { Environment, ClientConfig };
        private IFunctions _funcs;
        private u.Config _config;

        //construct
        public FinPlusClienView()
        {
            InitializeComponent();
            _funcs = new Functions();
        }

        public FinPlusClienView(Assembly assembly, bool hasSplash = true)
        {
            InitializeComponent();
          
            var configPath = assembly.Location.ToLower().Replace(".exe", "");
            var returnFunction = assembly.FullName.ToString().Split(',')[0];

            Initialise(configPath, returnFunction, string.Empty, hasSplash);

            IsValid = true;
        }

        public FinPlusClienView(string configPath, string name, string parms, u.Env env = u.Env.unknown, bool hasSplash = true)
        {
            InitializeComponent();

            Config = Name = name;

            if(env != u.Env.unknown) Env = env;

            var envPerm = GetPermission(Params.Environment.ToString(), Env.ToString());
            var configPerm = GetPermission(Params.ClientConfig.ToString(), name);

            if ((envPerm + configPerm).CanRead)
            {
                Initialise(configPath, name, parms, hasSplash);
                IsValid = true;
            }
            else
            {
                Dock(new FinPlusErrorDisplay(new Exception(string.Format("User {0} permission problem, 'Environment' ({1} {2}) 'ClientConfig' ({3} {4})", User, Env.ToString(), envPerm.ToString(), name, configPerm.ToString()))), "");
                IsValid = false;
            }
        }

        //common control interface 
        public override void Dock(object control, string dock)
        {
            try
            {
                switch(u.EnumTryParse<DockControls>(control.GetType().Name, DockControls.Default))
                {
                    case DockControls.Default: AddControl((FinPlusControl)control); break;
                    case DockControls.Exception: grid.Children.Add(new FinPlusErrorDisplay((Exception)control)); break;
                }
            }
            catch (Exception e)
            {
                grid.Children.Add(new FinPlusErrorDisplay(e));
                IsValid = log.Log(Logger.Level.Error, Id, Config, e.Message, e);
            }
        }

        public override void Dispose()
        {
            try
            {
                Assembler.Inst.Dispose(_funcs);
            }
            catch (Exception e)
            {
                log.Log(Logger.Level.Error, Id, Config, e.Message, e);
            }
        }

        //private
        private void Initialise(string configPath, string name, string parms, bool hasSplash)
        {
            try
            {
                u.Env env;
                _config = u.ConfigXml(configPath + ".Config", out env);
                log.Initialise(configPath + ".Config", env.ToString());
                
                Env = env;
                Config = name;

                if(hasSplash) new SplashScreen("splash.png").Show(true, true);

                var funcDockPanel = Assembler.Inst.Construct(_funcs = new Functions(configPath + ".Client", "root/current", parms), name, env);

                grid.Children.Add((UIElement)funcDockPanel.Object);
            }
            catch (Exception e)
            {
                grid.Children.Add(new FinPlusErrorDisplay(e));
                IsValid = log.Log(Logger.Level.Error, Id, Config, e.Message, e);
            }
        }

        private void AddControl(FinPlusControl control)
        {
            grid.Children.Add(control);
            if(_funcs != null)
                _funcs.Add(new Function(control.GetType().Name, control.Id){Object = control}, true, false);
        }
    }
}














































































































































































//Copyright2013<jnrton>AllRightsReserved 
//Contact j.nrton@finpus